home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / a86v400.zip / A10.DOC < prev    next >
Text File  |  1994-12-21  |  33KB  |  689 lines

  1. CHAPTER 10   RELOCATION AND LINKAGE
  2.  
  3.  
  4. A86 allows you to produce either .COM files, which can be run
  5. immediately as standalone programs, or .OBJ files, to be fed to
  6. the MS-DOS LINK program.  In this chapter I'll discuss .OBJ mode
  7. of A86.
  8.  
  9.  
  10. .OBJ Production Made Easy
  11.  
  12. I'll start by giving you the minimum amount of information you
  13. need to know to produce .OBJ files.  If you are writing short
  14. interface routines, and do not want to concern yourself with the
  15. esoterica of .OBJ files (segments, groups, publics, etc.), you
  16. can survive quite nicely by reading only this section.
  17.  
  18. There are two ways you can cause A86 to produce a .OBJ file as
  19. its object output.  One way is to explicitly give .OBJ as the
  20. output file name: for example, you can assemble the source file
  21. FOO.8 by giving the command "A86 FOO.8 FOO.OBJ".  The other way
  22. is to specify the switch +O (letter O not digit 0).  This is
  23. illustrated by the invocation "A86 +O FOO.8", which will have the
  24. same effect as the first invocation.
  25.  
  26. My design philosophy for .OBJ production is to accommodate two
  27. types of user.  The first type of user is writing new code, to
  28. link to other (usually high level language) modules.  That person
  29. should be able to write the module with a minimum of red tape,
  30. and have A86 do the right thing.  The second type of user has
  31. existing modules written for Intel/IBM assemblers, and wants to
  32. port them to A86.  A86 should recognize and act upon all the
  33. relocation directives (SEGMENT, GROUP, PUBLIC, EXTRN, NAME, END)
  34. given.  The assembly should work even if several files, assembled
  35. separately under the Intel/IBM assembler, are fed to a single A86
  36. assembly.  You'll see if you read on through this entire chapter
  37. that the multiple-files requirement causes A86 to interpret some
  38. of the relocation directives a little differently (while
  39. achieving compatible results).
  40.  
  41. Let's suppose you're writing new code: for example, an interface
  42. routine to the "C" language, that multiplies a 16-bit number by
  43. 10.  "C" pushes the input number onto the stack, before calling
  44. your routine.  Your code needs to get the number, multiply it by
  45. 10, and return the answer in the AX register.  You can code it:
  46.  
  47. _MUL10:          ; "C" expects all public names to start with "_"
  48.   PUSH BP        ; "C" expects BP to be preserved
  49.   MOV BP,SP      ; we use BP to address the stack
  50.   MOV AX,[BP+4]  ; fetch the number N, beyond BP and the ret addr
  51.   ADD AX,AX      ; 2N
  52.   MOV BX,AX      ; 2N is saved in BX
  53.   ADD AX,AX      ; 4N
  54.   ADD AX,AX      ; 8N
  55.   ADD AX,BX      ; 8N + 2N = 10N
  56.   POP BP         ; BP is restored
  57.   RET            ; go back to caller
  58.                                                              10-2
  59.  
  60. These 11 lines can be your entire source file!  If you name the
  61. file MUL10.8, A86 will create an object file MUL10.OBJ, that
  62. conforms to the standard SMALL model of computation for high
  63. level languages.  If you use RETF instead of RET (thus, by the
  64. way, getting the operand from BP+6 instead of BP+4), the object
  65. module will conform to the standard LARGE model of computation.
  66. All the red tape information required by the high level language
  67. is provided implicitly by A86.  I'll go through this information
  68. in detail later, but you should need to read about it only if
  69. you're curious.
  70.  
  71. What happens if you need to access symbols outside the module
  72. you're assembling?  If the type of the symbol is correctly
  73. guessed from the instruction that refers to it, then you can
  74. simply refer to it, and leave it undefined within the module. For
  75. example, if A86 sees the instruction CALL PRINT with PRINT
  76. undefined, it will assume that PRINT is a NEAR procedure.  If
  77. PRINT is never defined within the module, A86 will act as if you
  78. declared PRINT via the directive EXTRN PRINT:NEAR.  The address
  79. of PRINT will be plugged into your instruction by LINK when it
  80. combines A86's .OBJ file with the high level language's .OBJ
  81. files, to make the final program.
  82.  
  83. In general, the undefined operand to any CALL or JMP instruction
  84. is assumed to be NEAR.  The second (source) operand to a MOV or
  85. arithmetic instruction is assumed to be ABS (i.e., an immediate
  86. constant).  An undefined first (destination) operand is assumed
  87. to be a simple memory variable, of the same size (BYTE or WORD)
  88. as the register given in the second operand.  If your external
  89. symbol does not comply with these guidelines, you need to declare
  90. it with an EXTRN before you use it.  (You can also use EXTRN to
  91. declare types of non-complying forward references within your
  92. module, as you'll see later.)
  93.  
  94. If you'd like to link the MUL10 procedure to Turbo Pascal V4.0 or
  95. later, you need to append the line CODE SEGMENT PUBLIC to the top
  96. of the program, to name the program segment according to Turbo
  97. Pascal's expectations.  You may dispense with the leading
  98. underscore in the name MUL10-- Turbo Pascal does not require or
  99. expect it.
  100.  
  101. At this point, if you're a casual user, I think you've read
  102. enough to get going!  Read further only if you wish; or if you
  103. get stuck, and need to master the esoterica.
  104.                                                              10-3
  105.  
  106. Overview of Relocation and Linkage
  107.  
  108. When you assemble a program directly into a .COM file, the
  109. program has just two forms: the source program, that you can
  110. understand, and the .COM file, that the computer can "understand"
  111. (i.e., execute).  A .OBJ file is an intermediate format: neither
  112. you nor the (executing) computer can make sense out of a .OBJ
  113. file; only programs like LINK interpret .OBJ files.  The purpose
  114. of a .OBJ file is to allow you to assemble or compile just a part
  115. of a program.  The other parts (also in the form of .OBJ files)
  116. can be produced at a different time; often by a different
  117. assembler or compiler, whose source files are in a different
  118. language.  It's easy to see where the word "linkage" comes from:
  119. the LINK program puts the pieces of a program together.  The
  120. "relocation" comes because the assembler or compiler that makes a
  121. given program piece doesn't know how many other pieces will come
  122. before it, or how big the other pieces will be.  Each piece is
  123. constructed as if it started at location 0 within the program;
  124. then LINK "relocates" the piece to its true location.
  125.  
  126. Many of the relocation features of 86 assembly language are
  127. couched in terms of LINK's point of view, so we must look at the
  128. way LINK sees things.  LINK calls a .OBJ file an "object module",
  129. or just "module".  Each module has a NAME, that can be referred
  130. to when LINK issues diagnostic messages, such as error messages
  131. and symbol maps.  If a program symbol is used only within a
  132. single module, it does not need to be given to LINK, except
  133. possibly to pass along to a symbolic debugger.  On the other
  134. hand, if a program symbol is defined in one module and referenced
  135. in other modules, then LINK needs to know the name of the symbol,
  136. so it can resolve the references.  Such a symbol is PUBLIC in the
  137. module in which it is defined; it is "external" in the other
  138. modules, containing references to it.  Finally, exactly one
  139. module in a program must contain the starting location for the
  140. program; that module is called the "main module", and it must
  141. supply the starting address (which is not necessarily at the
  142. beginning of the module).
  143.  
  144. In the 86 family of microprocessors, the LINK system also does
  145. much to manage the memory segments that a program will fit into,
  146. and get its data from.  The (grotesquely ornate) level of support
  147. for segmentation was dictated by Intel, when it specified (and
  148. IBM and the compiler makers accepted) the format that .OBJ files
  149. will have.  I attended the fateful meeting at Intel, in which the
  150. crucial design decisions were made.  I regret to say that I sat
  151. quietly, while engineers more senior than I applied their fertile
  152. imaginations to construct fanciful scenarios which they felt had
  153. to be supported by LINK.  Let's now review the resulting
  154. segmentation model.
  155.                                                              10-4
  156.  
  157. The parts of a program, as viewed by LINK, come in three
  158. different sizes: they